//
//  comicMenuViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/12/15.
//  Copyright © 2016年 王少泽. All rights reserved.
//
#import "AppDelegate.h"

#import "ComicReadViewController.h"
#import "comicCommentView.h"
#import "comicMenuViewController.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>


#import "comicCollectionView.h"
#import "comicChapterTableViewCell.h"
#import "comicCommentTableViewCell.h"
#import "ChapterCollectionViewCell.h"

#import "XZBaseNetwork.h"

//宏定义宽度高度
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface comicMenuViewController ()<UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout,
UITableViewDelegate,
UITableViewDataSource>

@end

@implementation comicMenuViewController
{
    UITableView* _comicShowInfo;
    NSArray* _dataSource;
    UIView* _headView;
    UIView* _midView;
    UIView* _bottomView;
    UIScrollView* _menuView;
    comicCollectionView* _comicView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    //请求漫画列表数据
    [[XZBaseNetwork shareInstance] startWithGetURL:@"http://japi.juhe.cn/comic/chapter" params:
  @{@"comicName":_nameTitle,
    @"skip":@"",
    @"key":@"e4c2c6f92b486c97e01328b1bc54c913"} success:^(id success) {
        
        _dataSource = [[success objectForKey:@"result"] objectForKey:@"chapterList"];
        NSLog(@"_dataSourrrrrce:%@",_dataSource);
        [_comicView.comicCollectionView reloadData];
        
    } failure:^(id failure) {
        
    }];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self lazyMod];
    
//    [self.view addSubview:_comicShowInfo];
    self.navigationItem.title = self.nameTitle;
    
   
    [self.view addSubview:[self lazyLoadMenu]];
}
#pragma mark 懒加载模式
//- (UITableView* )lazyMod
//{
//    if (_comicShowInfo == nil) {
//        _comicShowInfo = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//        _comicShowInfo.dataSource = self;
//        _comicShowInfo.delegate = self;
//        
//        [_comicShowInfo registerNib:[UINib nibWithNibName:NSStringFromClass([comicInfoTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"topCell"];
//        
//        [_comicShowInfo registerNib:[UINib nibWithNibName:NSStringFromClass([comicChapterTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"midCell"];
//        
//        [_comicShowInfo registerNib:[UINib nibWithNibName:NSStringFromClass([comicCommentTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"bottomCell"];
//        
//    }
//    return _comicShowInfo;
//}

- (UIScrollView* )lazyLoadMenu
{
    if (_menuView == nil) {
        _menuView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
        _menuView.contentSize = CGSizeMake(WIDTH, HEIGHT + 50);
        _menuView.backgroundColor = [UIColor grayColor];
        [_menuView addSubview:[self lazyLoadHeadView]];
        [_menuView addSubview:[self lazyLoadMidView]];
        [_menuView addSubview:[self lazyLoadBottom]];
        
    }
    return _menuView;
}

- (UIView* )lazyLoadHeadView
{
    if (_headView == nil) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
        _headView.backgroundColor = [UIColor colorWithRed:32/255.0 green:194/255.0 blue:219/255.0 alpha:1];
        //设置封面
        UIImageView* coverImage = [[UIImageView alloc]initWithFrame:CGRectMake(8, 8, 67, 85)];
        [coverImage sd_setImageWithURL:self.coverImage placeholderImage:[UIImage imageNamed:@"backView.jpg"]];
        [_headView addSubview:coverImage];
        NSArray* tmpImage = @[@"coustmer",@"tag",@"tag",@"tag",@"time"];
        NSArray* tmpTitle = @[@"作者",@"分类",@"人气",@"订阅",@"更新时间与是否连载"];
        for (NSInteger i = 0; i < 5; i++) {
            UIButton* tmp = [UIButton buttonWithType:UIButtonTypeCustom];
            [tmp setImage:[UIImage imageNamed:tmpImage[i]] forState:UIControlStateNormal];
            [tmp setTitle:tmpTitle[i] forState:UIControlStateNormal];
            tmp.frame = CGRectMake(98, 8+i * 19, WIDTH, 19);
            tmp.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
            [_headView addSubview:tmp];
        }
        UILabel* des = [[UILabel alloc]initWithFrame:CGRectMake(0, 119, WIDTH, 90)];
        des.text = _comicSynopsis;
        des.backgroundColor = [UIColor blueColor];
        
        [_headView addSubview:des];
    }
    return _headView;
}

- (UIView* )lazyLoadMidView
{
    if (_midView == nil) {
        _midView = [[UIView alloc]initWithFrame:CGRectMake(0, 214, WIDTH, 150)];
        _midView.backgroundColor = [UIColor redColor];
        _comicView = _comicView =[[comicCollectionView alloc]initWithFrame:CGRectMake(0, 20, _midView.frame.size.width, _midView.frame.size.height - 20)];
        _comicView.comicCollectionView.delegate = self;
        _comicView.comicCollectionView.dataSource = self;
        [_midView addSubview:_comicView];
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:@"测试按钮" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addYrange:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(WIDTH - 100, 90, 90, 20);
        [_midView addSubview:button];
    }

    return _midView;
}

- (UIView* )lazyLoadBottom
{
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 374, WIDTH, 243)];
        _bottomView.backgroundColor = [UIColor whiteColor];
        comicCommentView* comment = [[comicCommentView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 223)];
        comment.commentTable.dataSource = self;
        comment.commentTable.delegate = self;
        
        [_bottomView addSubview:comment];
        
        CGRect frame = comment.commentTable.tableHeaderView.frame;
        frame.size.height = 1;
        UIView* headView = [[UIView alloc]initWithFrame:frame];
        [comment.commentTable setTableHeaderView:headView];
    }
    return _bottomView;
}

- (void)addYrange:(UIButton* )sender
{
    CGRect frame = CGRectMake(0, 214, WIDTH, 150);
    if (_midView.frame.size.height == frame.size.height) {
        _midView.frame = CGRectMake(0, 214, WIDTH, 450);
        _bottomView.frame = CGRectMake(0, 674, WIDTH, 543);
        _menuView.contentSize = CGSizeMake(WIDTH, HEIGHT + 350);
    }
    else
    {
        _midView.frame = CGRectMake(0, 214, WIDTH, 150);
        _bottomView.frame = CGRectMake(0, 374, WIDTH, 243);
        _menuView.contentSize = CGSizeMake(WIDTH, HEIGHT+ 50);
    }
    
}

#pragma mark 代理方法的实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-  (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    comicCommentTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark UICollectionView代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //    return _dataSource.count;
    return _dataSource.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChapterCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"comicCell" forIndexPath:indexPath];
    
    cell.titleText.backgroundColor = [UIColor redColor];
    NSLog(@"nnnname:%@",[[_dataSource objectAtIndex:indexPath.row] objectForKey:@"name"]);
    cell.titleText.text = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ComicReadViewController* readVC = [[ComicReadViewController alloc] init];
    readVC.comicId = [[[_dataSource objectAtIndex:indexPath.row] objectForKey:@"id"] intValue];
    readVC.comicName = self.nameTitle;
    readVC.comicChapter = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"name"];
    [self.navigationController pushViewController:readVC animated:NO];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
